//
//  ChatBarVC.m
//  HXDemo
//
//  Created by 李亚军 on 16/11/21.
//  Copyright © 2016年 李亚军. All rights reserved.
//

#import "ChatBarVC.h"
#import <QuartzCore/QuartzCore.h>
#import "BoardButton.h"
#import "BoardTextView.h"


@interface ChatBarVC ()<UITextViewDelegate>

/// 是否显示聊天bar工具
@property(nonatomic , assign) BOOL isShowChatBarTool;

/// 是否处于编辑状态
@property(nonatomic , assign) BOOL isEditing;

/// 点击按钮 0 : 语音 ; 1 : 表情 ; 2 : 工具
@property(nonatomic , copy) NSString *clickType;

/// 当前输入的textView值
@property(nonatomic , copy) NSString *currentInputText;


/// 是否显示按住讲话按钮
@property(nonatomic , assign) BOOL isShowPressSpeaking;

/// 聊天bar 的高度
@property(nonatomic , assign) CGFloat chatBarHeight;

@property(nonatomic , strong) NSTimer *timer;

@property(nonatomic , assign) NSInteger timerInterval;

@end

@implementation ChatBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initDefaultAttributes];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initDefaultAttributes];

}

/// 初始化默认属性
- (void)initDefaultAttributes {

    /// 默认没有显示聊天bar的工具栏
    self.isShowChatBarTool = NO;
    
    /// 默认设置
    self.isEditing = NO;
    
    /// 按住讲话 按钮 默认隐藏
    self.pressSpeaking.alpha = 0;
    self.isShowPressSpeaking = NO;
    self.chatBarHeight = 50;
    
    
    self.timerInterval = 0;
    
    [self initTimer];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    NSLog(@"开始编辑");
    self.isEditing = YES;

}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length > 500) {
        textView.text = self.currentInputText;
        return;
    }
    
    self.currentInputText = textView.text;

    CGSize size = [self calculateWidthOfString:self.currentInputText fontSize:14 labelH:0 labelW:([UIScreen mainScreen].bounds.size.width - 128)];

    self.chatBarHeight = size.height + 32;
    self.chatBarHeight = self.chatBarHeight < 50 ? 50 : self.chatBarHeight + 5;
    
    if (self.chatBarHeight > 105) {
        self.chatBarHeight = 105;
    }else {
        
        if (self.chatBarHeight < 50) {
            self.chatBarHeight = 50;
        }
    }
    
    [self refreshUI];
}

/// 如果文字超出一行进行换行刷新UI
- (void)refreshUI {
    
    self.chatBarChatViewHeight.constant = self.chatBarHeight;
    
    [self.view layoutIfNeeded];
    
    CGFloat heightOfFontToIncrease = self.chatBarHeight - 50; // textView 字体增加的高度
    if (self.textViewChangeBlock) {
        self.textViewChangeBlock(heightOfFontToIncrease);
    }
}

- (CGSize)calculateWidthOfString:(NSString *)str fontSize:(NSInteger)fontSize labelH:(NSInteger)labelH labelW:(NSInteger)labelW {
    NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
    CGSize size = [str boundingRectWithSize:CGSizeMake(labelW, labelH) options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    return size;
}


- (void)textViewDidEndEditing:(UITextView *)textView {
    
    NSLog(@"结束编辑");
    self.isEditing = NO;
    self.isShowChatBarTool = NO;
}

#pragma mark - action
/// 语音 action
- (IBAction)voiceAction:(UIButton *)sender {
    
    [self.emotionButton setBackgroundColor:[UIColor clearColor]];
    [self.toolButton setBackgroundColor:[UIColor clearColor]];
    
    if (!self.isShowPressSpeaking) { // 如果为NO , 代表没有显示
        
        self.pressSpeaking.alpha = 1;
        self.chatBarChatViewHeight.constant = 50;
        
        if (self.isEditing) {
            
            self.hiddenKeyboardBlock(@"0");
            self.isShowChatBarTool = NO;
            self.isEditing = NO;
        }else {
            
            if (self.isShowChatBarTool) {
                
                if (self.hiddenToolBarBlock) {
                    self.hiddenToolBarBlock();
                    
                    [UIView animateWithDuration:0.3 animations:^{
                        
                        self.emotionMarginToBottom.constant = -180;
                        self.toolMarginToBottom.constant = -180;
                        [self.view layoutIfNeeded];
                    }];
                    self.isShowChatBarTool = NO;
                    self.isEditing = NO;
                    
                }
            }
        }
        
        [self.textView resignFirstResponder];
        self.isShowPressSpeaking = !self.isShowPressSpeaking;

    }else {

        self.pressSpeaking.alpha = 0;
        [self.textView becomeFirstResponder];
        self.chatBarChatViewHeight.constant = self.chatBarHeight;
        self.isShowPressSpeaking = !self.isShowPressSpeaking;
        [self refreshUI];

    }
}

/// 表情 action
- (IBAction)emotionAction:(UIButton *)sender {
    
    [self refreshUI];
    self.pressSpeaking.alpha = 0;
    self.isShowPressSpeaking = NO;
    
    if (![self.clickType isEqualToString:@"1"] && self.isShowChatBarTool) {
        self.isShowChatBarTool = NO;
        [self.toolButton setBackgroundColor:[UIColor clearColor]];
    }

    if (self.isEditing) {
        
        self.hiddenKeyboardBlock(@"1");
        
        self.isShowChatBarTool = YES;
        [sender setBackgroundColor:[UIColor lightGrayColor]];
        [self.toolButton setBackgroundColor:[UIColor clearColor]];
        
        
        [UIView animateWithDuration:0.3 animations:^{
        
            
            self.toolMarginToBottom.constant = -230;
            self.emotionMarginToBottom.constant = 0;
            [self.view layoutIfNeeded];
            
            
        }];
        
        self.clickType = @"1";
        
        return;
    }
    
    if (!self.isShowChatBarTool) {
        
        if (self.showToolBarBlock) {
            self.showToolBarBlock();
            [sender setBackgroundColor:[UIColor lightGrayColor]];
            [self.toolButton setBackgroundColor:[UIColor clearColor]];
            
            [UIView animateWithDuration:0.3 animations:^{
                
                self.emotionMarginToBottom.constant = 0;
                self.toolMarginToBottom.constant = -180;
                [self.view layoutIfNeeded];
            }];
            
        }
        self.isShowChatBarTool = !self.isShowChatBarTool;
    }else {
        
        if (self.hiddenToolBarBlock) {
            self.hiddenToolBarBlock();
            [sender setBackgroundColor:[UIColor clearColor]];
            [self.toolButton setBackgroundColor:[UIColor clearColor]];
            
            [UIView animateWithDuration:0.3 animations:^{
                
                self.emotionMarginToBottom.constant = -180;
                self.toolMarginToBottom.constant = -180;
                [self.view layoutIfNeeded];
            }];
        }
        
        self.isShowChatBarTool = !self.isShowChatBarTool;
    }
    
    self.clickType = @"1";
}

/// 工具 action
- (IBAction)toolAction:(UIButton *)sender {
    
    [self refreshUI];
    self.pressSpeaking.alpha = 0;
    self.isShowPressSpeaking = NO;
    
    [self.emotionButton setBackgroundColor:[UIColor clearColor]];
    
    if (![self.clickType isEqualToString:@"2"] && self.isShowChatBarTool) {
        self.isShowChatBarTool = NO;
        [self.emotionButton setBackgroundColor:[UIColor clearColor]];
    }
    
    if (self.isEditing) {
        
        self.hiddenKeyboardBlock(@"2");
        
        self.isShowChatBarTool = YES;
        [sender setBackgroundColor:[UIColor lightGrayColor]];

        [UIView animateWithDuration:0.3 animations:^{
        
            self.toolMarginToBottom.constant = 0;
            self.emotionMarginToBottom.constant = - 230;
            [self.view layoutIfNeeded];
        }];
        
        self.clickType = @"2";

        return;
    }
    
    if (!self.isShowChatBarTool) {
        
        if (self.showToolBarBlock) {
            self.showToolBarBlock();
            [sender setBackgroundColor:[UIColor lightGrayColor]];
            
            [UIView animateWithDuration:0.3 animations:^{
                
                
                self.toolMarginToBottom.constant = 0;
                self.emotionMarginToBottom.constant = - 180;
                [self.view layoutIfNeeded];
            }];

        }
        self.isShowChatBarTool = !self.isShowChatBarTool;
    }else {
        
        if (self.hiddenToolBarBlock) {
            self.hiddenToolBarBlock();
            [sender setBackgroundColor:[UIColor clearColor]];
            [self.emotionButton setBackgroundColor:[UIColor clearColor]];
            
            [UIView animateWithDuration:0.3 animations:^{
                
                self.emotionMarginToBottom.constant = -180;
                self.toolMarginToBottom.constant = -180;
                [self.view layoutIfNeeded];
            }];
        }
        
        self.isShowChatBarTool = !self.isShowChatBarTool;
    }
    
    self.clickType = @"2";
}

/// 抬起action
- (IBAction)pressAction:(BoardButton *)sender {
    
    NSLog(@"抬起--时间:%ld" , (long)self.timerInterval);
    
    self.timer = 0;
    [self.timer invalidate];
}

- (IBAction)pressDownAction:(BoardButton *)sender {
    
    NSLog(@"按下");
    self.timerInterval = 0;
    [self.timer fire];
}

/// 初始化时间
- (void)initTimer {
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(beginTiming) userInfo:nil repeats:YES];
}

/// 开始计时
- (void)beginTiming {
    
    self.timerInterval += 1;
}

- (void)dealloc {
    
    [self.timer invalidate];
    self.timer = nil;
    NSLog(@"销毁了");
}

@end
